/*
+--------------------------------------------------------------------------
|   Mblog [#RELEASE_VERSION#]
|   ========================================
|   Copyright (c) 2014, 2015 mtons. All Rights Reserved
|   http://www.mtons.com
|
+---------------------------------------------------------------------------
*/
package com.hefei.blog.web.controller.desk.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hefei.blog.base.lang.Consts;
import com.hefei.blog.base.lang.EnumPrivacy;
import com.hefei.blog.base.paging.Paging;
import com.hefei.blog.core.data.User;
import com.hefei.blog.core.persist.service.CommentService;
import com.hefei.blog.core.persist.service.FavorService;
import com.hefei.blog.core.persist.service.FeedsService;
import com.hefei.blog.core.persist.service.FollowService;
import com.hefei.blog.core.persist.service.NotifyService;
import com.hefei.blog.core.persist.service.PostService;
import com.hefei.blog.core.persist.service.UserService;
import com.hefei.blog.web.consts.Views;
import com.hefei.blog.web.controller.BaseController;
import com.hefei.frontend.framework.http.request.CommonParameterThreadLocal;

/**
 * 用户主页
 * @author langhsu
 *
 */
@Controller
public class HomeController extends BaseController {
	
	private Logger logger = Logger.getLogger(HomeController.class);
	
	@Autowired
	private PostService postService;
	@Autowired
	private FeedsService feedsService;
	@Autowired
	private CommentService commentService;
	@Autowired
	private UserService userService;
	@Autowired
	private FollowService followService;
	@Autowired
	private FavorService favorService;
	@Autowired
	private NotifyService notifyService;

	/**
	 * 用户主页
	 * @param pn
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/home")
	public String home(Integer pn, ModelMap model, HttpServletRequest request) {
		Paging paging = wrapPage(pn);

		feedsService.findUserFeeds(paging, CommonParameterThreadLocal.getUserId(), Consts.ZERO, Consts.ZERO);

		model.put("page", paging);
		initUser(model,request);

		return getView(Views.HOME_FEEDS);
	}

	/**
	 * 我发布的文章
	 * @param pn
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/home", params = "method=posts")
	public String posts(Integer pn, ModelMap model, HttpServletRequest request) {
		Paging page = wrapPage(pn);
//		UserProfile up = getSubject(request);
		postService.pagingByAuthorId(page, CommonParameterThreadLocal.getUserId(), EnumPrivacy.ALL);

		model.put("page", page);
		initUser(model,request);

		return getView(Views.HOME_POSTS);
	}

	/**
	 * 我发表的评论
	 * @param pn
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/home", params = "method=comments")
	public String comments(Integer pn, ModelMap model, HttpServletRequest request) {
		Paging page = wrapPage(pn);
		page = commentService.paging4Home(page, CommonParameterThreadLocal.getUserId());

		model.put("page", page);
		initUser(model,request);

		return getView(Views.HOME_COMMENTS);
	}

	/**
	 * 我喜欢过的文章
	 * @param pn
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/home/favors")
	public String favors(Integer pn, ModelMap model, HttpServletRequest request) {
		Paging page = wrapPage(pn);
//		UserProfile profile = getSubject(request);
		favorService.pagingByOwnId(page, CommonParameterThreadLocal.getUserId());

		model.put("page", page);
		initUser(model,request);

		return getView(Views.HOME_FAVORS);
	}

	/**
	 * 我的关注
	 * @param pn
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/home/follows")
	public String follows(Integer pn, ModelMap model, HttpServletRequest request) {
		Paging page = wrapPage(pn);
//		UserProfile profile = getSubject(request);
		followService.follows(page, CommonParameterThreadLocal.getUserId());

		model.put("page", page);
		initUser(model,request);

		return getView(Views.HOME_FOLLOWS);
	}

	/**
	 * 我的粉丝
	 * @param pn
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/home/fans")
	public String fans(Integer pn, ModelMap model, HttpServletRequest request) {
		Paging page = wrapPage(pn);
//		UserProfile profile = getSubject(request);
		followService.fans(page, CommonParameterThreadLocal.getUserId());

		model.put("page", page);
		initUser(model,request);

		return getView(Views.HOME_FANS);
	}

	/**
	 * 我的通知
	 * @param pn
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/home/notifies")
	public String notifies(Integer pn, ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		Paging page = wrapPage(pn);
//		UserProfile profile = getSubject(request);
		notifyService.findByOwnId(page, CommonParameterThreadLocal.getUserId());
		// 标记已读
		notifyService.readed4Me(CommonParameterThreadLocal.getUserId());

		model.put("page", page);
		initUser(model,request);

		pushBadgesCount(request, response);

		return getView(Views.HOME_NOTIFIES);
	}

	private void initUser(ModelMap model, HttpServletRequest request) {
//		UserProfile up = getSubject(request);
		User user = userService.get(CommonParameterThreadLocal.getUserId());

		model.put("user", user);
	}

	private void pushBadgesCount(HttpServletRequest request, HttpServletResponse response) {
//		AccountProfile profile = super.getSubject(request);
//		if (profile != null && profile.getBadgesCount() != null) {
//			profile.getBadgesCount().setNotifies(0);
//			request.getSession().setAttribute("profile", profile);
//			
//			try {
//				putProfile(profile, request, response);
//			} catch (Exception e) {
//				logger.error("error", e);
//			}
//		}
	}
}
